/*
 * Utils.java
 *
 * Copyright (c) 2008 Teamnet. All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of Teamnet.
 */
package ro.prjmanager.bundle.util;

import ro.prjmanager.bundle.BundleClReader;
import static ro.prjmanager.constants.BackendConstants.*;

import java.util.StringTokenizer;
import java.util.List;

public class Utils {
    private static List<String> restrictedSuffix;
    public static String dataBaseNameToJavaNameClassStyle(String dataBaseName){
        if(restrictedSuffix==null||restrictedSuffix.isEmpty())
            restrictedSuffix= BundleClReader.getInstance(RESTR_SUFF_CONFIG_PATH+"/"+RESTR_SUFF_CONFIG_NAME).
                    getCollectionString(RESTR_KEY_PATTERN,20);
        if(dataBaseName==null)
         return null;
       StringBuilder sb=new StringBuilder();
        if(restrictedSuffix!=null&&!restrictedSuffix.isEmpty()){
            for(String restr:restrictedSuffix)
                if(dataBaseName.toUpperCase().startsWith(restr.toUpperCase()))
                    dataBaseName=dataBaseName.substring(restr.length());
        }

       StringTokenizer st=new StringTokenizer(dataBaseName,"_");
       while(st.hasMoreTokens()){
           String el=st.nextToken();

           sb.append(el.substring(0,1).toUpperCase());
           sb.append(el.substring(1).toLowerCase());
       }
       return sb.toString();
   }

    public static String dataBaseNameToJavaNameAttributeStyle(String dataBaseName){
        if(dataBaseName==null)
            return null;
        if(restrictedSuffix==null||restrictedSuffix.isEmpty())
            restrictedSuffix= BundleClReader.getInstance(RESTR_SUFF_CONFIG_PATH+"/"+RESTR_SUFF_CONFIG_PATH).
                    getCollectionString(RESTR_KEY_PATTERN,20);

      
        if(restrictedSuffix!=null&&!restrictedSuffix.isEmpty()){
            for(String restr:restrictedSuffix)
                if(dataBaseName.toUpperCase().startsWith(restr.toUpperCase()))
                    dataBaseName=dataBaseName.substring(restr.length());
        }
        StringBuilder sb=new StringBuilder();
        String str=dataBaseNameToJavaNameClassStyle(dataBaseName);
        sb.append(str.substring(0,1).toLowerCase());
        sb.append(str.substring(1));
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(dataBaseNameToJavaNameClassStyle("MAMA_ARE_MERE"));
        System.out.println(dataBaseNameToJavaNameAttributeStyle("MAMA_ARE_MERE"));
    }
}
